package parts.spring;

import java.awt.geom.Point2D;
import applet.Simulation;
import applet.forces.Force;
import parts.mass.Mass;


/**
 * @author Guy Tracy and Jesse Starr
 *
 * Bar is called as a subclass of Spring, in which a high K
 * value is used. The displacement is determined by the formula for
 * a critically damped harmonic oscillator. Ideally, the bar should
 * not oscillate at all during the simulation.
 */
public class Bar extends Spring {

    private final static double myBarKVal = 5;
    private final double myInitialLen;

    /**
     * Creates a bar.
     *
     * @param start first mass it is connected to
     * @param end second mass it is connected to
     * @param length of the bar
     */
    public Bar (Mass start, Mass end, double length) {
        super(start, end, length, myBarKVal);
        myInitialLen = length;
    }

    @Override
    public void update (Simulation canvas, double dt) {
        double newlen;
        double damping;
        double reducedMass = (getMyStart().getMass() * getMyEnd().getMass())
                / (getMyStart().getMass() + getMyEnd().getMass());

        damping = Math.sqrt(getMyK() / reducedMass);
        newlen = (myInitialLen + (damping * myInitialLen * dt))
                    * Math.pow(Math.E, -1 * damping * dt);

        setMyLength(newlen);
        super.update(canvas, dt);
    }
}
